<?php
/**
 * --------------------------------------------------------------
 * 角色控制器
 * --------------------------------------------------------------
 * Light Year Doc
 * @author yinq<3331653644@qq.com>
 * --------------------------------------------------------------
 */
namespace app\admin\controller;

use app\common\controller\AdminBase;
use app\admin\model\AuthGroup as AuthGroupModel;
use app\admin\model\AuthRule as AuthRuleModel;

class AuthGroup extends AdminBase
{
    /**
     * 角色列表
     */
    public function index()
    {
        $map  = $this->getSimpleSearchMap();
        $list = AuthGroupModel::where($map['where'])
                ->order('id desc')
                ->paginate(config('app.site_setting.list_rows'), false, [
                    'query' => $map['conditions']
                ]);

        $this->assign([
            'list'         => $list,
            'keyword'      => $map['conditions']['keyword'],
            'search_field' => $map['conditions']['search_field'],
            'title'        => '后台角色列表'
        ]);
        return $this->fetch();
    }

    /**
     * 新增角色
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $data     = $this->request->post();
			
			$result   = $this->validate($data, 'AuthGroup');
            if (true !== $result) $this->error($result);
			
            $data['rules'] = empty($data['rules']) ? '' : implode(',', $data['rules']);
			if (AuthGroupModel::create($data)) {
			    return $this->success('新增成功', url('index'));
			} else {
			    return $this->error('新增失败');
			}
        } else {
            $rules = AuthRuleModel::queryOneOrList(['status' => 1], 'id,title,type,pid', 'list', 'sort asc');
            $rules = list_to_tree_two($rules);

        	$this->assign([
                'rules' => $rules,
                'title' => '新增角色'
            ]);
        	return $this->fetch();
        }
    }

	/**
     * 编辑角色
     * @author yinq
	 */
	public function edit($id = 0)
	{
        if ($id <= 0 || !is_numeric($id)) $this->error('参数错误');
        if ($this->request->isPost()) {
            $data     = $this->request->post();
			
			$result   = $this->validate($data, 'AuthGroup');
            if (true !== $result) $this->error($result);
			
			$data['rules'] = empty($data['rules']) ? '' : implode(',', $data['rules']);
			
			if (AuthGroupModel::update($data)) {
			    return $this->success('编辑成功', url('index'));
			} else {
			    return $this->error('编辑失败');
			}
        } else {
            $rules         = AuthRuleModel::queryOneOrList(['status' => 1], 'id,title,type,pid', 'list', 'sort asc');
            $rules         = list_to_tree_two($rules);
        	$info          = AuthGroupModel::get($id);
			$info['rules'] = explode(',', $info['rules']);

        	$this->assign([
			    'info'  => $info,
		        'rules' => $rules,
                'title' => '编辑角色'
			]);
        	return $this->fetch();
        }
	}

	/**
     * 删除角色
	 */
	public function delete($ids = 0)
	{
        (int) $ids || $this->error('参数错误');
        in_array('1', $ids) && $this->error('不能删除超级管理员角色');
        
        $map    = ['id' => $ids];
        $result = AuthGroupModel::where($map)->delete();
		// 删除角色是否需要删除用户与角色的对应表?
        return $result ? $this->success('删除成功!') : $this->error('删除失败!');
	}
	
	/**
	 * 禁用/启用
	 * @param array $ids id数组
     * @param integer $status 状态值
     */
    public function setStatus($ids = [], $status)
    {
        empty($ids) && $this->error('请选择要操作的数据');
        !is_numeric((int) $status) && $this->error('参数错误');
        in_array('1', $ids) && $this->error('不能禁用超级管理员角色');
        
        $map  = ['id' => $ids];
        $info = AuthGroupModel::where($map)->setField('status', $status);
        $tips = $status == 1 ? '启用' : '禁用';
        return $info !== FALSE ? $this->success($tips.'成功') : $this->error($tips.'失败');
    }
}